package com.clock.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.clock.common.constant.FollowCacheKey;
import com.clock.common.domain.R;
import com.clock.common.utils.RedisUtil;
import com.clock.domain.dto.UpdateUserPasswordDTO;
import com.clock.domain.dto.UserByManagerDTO;
import com.clock.domain.entity.User;
import com.clock.domain.vo.FollowsInfVO;
import com.clock.domain.vo.UserBackVo;
import com.clock.mapper.UserMapper;
import com.clock.service.FollowService;
import com.clock.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    RedisUtil redisUtil;


    @Override
    public User login(User user) {
        if(user.getUserId()==null && user.getUserTel()==null && user.getUserName()==null){
            return null;
        }
        User user1 = userMapper.queryByUserInfAndPassword(user);
        return user1;
    }

    @Override
    public String userInfCheck(User user) {
        if(user.getUserName()!=null){
            int i = userMapper.queryByUserName(user.getUserName());
            if(i>0){
                return "用户名已存在";
            }
        }

        if(user.getUserTel()!=null){
            if(!Validator.isMobile(user.getUserTel())){
                return "手机号错误";
            }
            int i = userMapper.queryByUserTel(user.getUserTel());
            if(i>0){
                return "手机号已存在";
            }
        }

        if(user.getUserEmail()!=null){
            if(!Validator.isEmail(user.getUserEmail())){
                return "邮箱错误";
            }
            int i = userMapper.queryByUserEmail(user.getUserEmail());
            if(i>0){
                return "邮箱已存在";
            }
        }


        return null;
    }

    @Override
    public Integer getRoleId(Long userId) {
        User user = userMapper.queryById(userId.intValue());
        return user.getUserRoleId();
    }

    @Override
    public PageInfo<User> getAllUser(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = userMapper.queryAllUser();
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return pageInfo;
    }

    @Override
    public Boolean updatePassword(UpdateUserPasswordDTO updateUserPasswordDTO) {
        int updateUserPassword = userMapper.updateUserPassword(updateUserPasswordDTO.getUserId(),
                updateUserPasswordDTO.getUserOldPassword(),
                updateUserPasswordDTO.getUserNewPassword());
        return updateUserPassword>0;
    }

    @Override
    public List<FollowsInfVO> getFellowInf(User user) {
        List<FollowsInfVO> list = new ArrayList<>();
        String followCacheKey = FollowCacheKey.FOLLOW_PRE_CACHE_KEY + user.getUserId();
        Set<Object> ids = redisUtil.sGet(followCacheKey);

        for (Object idObject : ids) {
            Integer id = (Integer) idObject;
            FollowsInfVO followsInfVO = new FollowsInfVO();
            followsInfVO.setFollowsId((long)id.intValue());

        }

        return null;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userMapper.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<User> queryByPage(User user, PageRequest pageRequest) {
        long total = this.userMapper.count(user);
        return new PageImpl<>(this.userMapper.queryAllByLimit(user, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public User insert(User user) {
        if(user.getUserPassword() == null || user.getUserName() ==null){
            return null;
        }
        if(user.getUserCreateTime()==null){
            user.setUserCreateTime(DateUtil.date());
        }
        if(user.getUserStudyAllTime()==null)  {
            user.setUserStudyAllTime(0L);
        }
        if(user.getUserStudyAllTime() == null){
            user.setUserStudyAllTime(0l);
        }

        user.setUserRoleId(3);

        String check = this.userInfCheck(user);
        if(check!=null){
            return null;
        }

        int i = this.userMapper.insert(user);
        if(i<=0){
            return null;
        }

        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public String update(User user) {
        String infCheck = userInfCheck(user);
        if(infCheck == null){
            int i = this.userMapper.update(user);
            if(i>0){
                return null;
            }else{
                return "修改信息失败";
            }
        }else{
            return infCheck;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userMapper.deleteById(userId) > 0;
    }


}
